<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail" v-enterToNext>
        <div style="width: 140px; line-height: 32px; font-weight: bold; color: #000">{{ $t('乐娱商城') }}</div>
        <a-row>
          <a-col :span="24">
            <a-form-model-item :label="$t('通讯秘钥')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="apiKey">
              <a-input v-model="model.apiKey" :disabled="disable" style="width: 82%;"></a-input>
              <a-button @click="copy" style="background-color:#F96455;color:#ffffff;left: 5%;">{{$t('复制')}}</a-button>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('商城服务器')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="giftUrl">
              <a-input v-model="model.giftUrl" :placeholder="$t('请输入商城服务器')"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
        <div style="width: 140px; line-height: 32px; font-weight: bold; color: #000">{{ $t('哈哈转商城') }}</div>
        <a-row>
          <a-col :span="24">
            <a-form-model-item :label="$t('商户编号')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="hhzMerchantCode">
              <a-input v-model="model.hhzMerchantCode" :placeholder="$t('请输入商户编号')"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('场地编号')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="hhzYardCode">
              <a-input v-model="model.hhzYardCode" :placeholder="$t('请输入场地编号')"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'ShopManageGiftForm',
  components: {
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        giftUrl: [
          { required: false, message: this.$t('请输入商城服务器') }
        ],
        hhzMerchantCode: [
          { required: false, message: this.$t('请输入商户编号') }
        ],
        hhzYardCode: [
          { required: false, message: this.$t('请输入场地编号') }
        ]
      },
      url: {
        add: '/shop/bisShop/add',
        edit: '/shop/bisShop/edit',
        queryById: '/shop/bisShop/queryById',
      },
      disable: false,
      defaultValue: [],
      centerPoint: '',
      typeList:[],
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
      if (this.model.id) {
        this.disable = true
      }
    },
    copy(){
      let cInput = document.createElement("input");
      cInput.value = this.model.apiKey;
      document.body.appendChild(cInput);
      cInput.select();
      document.execCommand("copy")
      if(document.execCommand("copy")){
        this.$message.success(this.$t('复制成功'))
      }
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
  },
}
</script>